package com.yc.shadowdemo;

import android.util.Log;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.lang.annotation.Target;

/**
 * Creator: yc
 * Date: 2021/6/3 10:42
 * UseDes:
 */

public class MyUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
    @Override
    public void uncaughtException(Thread thread, Throwable ex) {
        final Writer result = new StringWriter();
        final PrintWriter printWriter = new PrintWriter(result);
        //如果异常时在AsyncTask里面的后台线程抛出的
        //那么实际的异常仍然可以通过getCause获得
        ex.printStackTrace();
//        Throwable cause = ex;
//        while (null != cause) {
//            cause.printStackTrace(printWriter);
//            cause = cause.getCause();
//        }
//        //stacktraceAsString就是获取的carsh堆栈信息
//        final String stacktraceAsString = result.toString();
//        Log.e("Exception", stacktraceAsString);
        printWriter.close();
    }
}